<script setup>
import { useOrderStore, useUserStore } from '@/stores'
import ReturnButton from '@/components/ReturnButton.vue'
import { ref, computed } from 'vue'

const userStore = useUserStore()
const orderStore = useOrderStore()
console.log(orderStore.orderList)
const filterOrderList = computed(() =>
  orderStore.orderList.filter(item => item.userId === userStore.userId)
)
const orderList = ref(filterOrderList.value)

// 格式化金额函数
const formatPrice = price => {
  return '¥' + (price ? Number(price).toFixed(2) : '0.00')
}

// 根据订单状态返回对应的样式类
const statusClass = status => {
  const statusMap = {
    待付款: 'status-pending',
    已支付: 'status-paid',
    待发货: 'status-processing',
    已发货: 'status-shipped',
    已完成: 'status-completed',
    已取消: 'status-canceled',
    退款中: 'status-refunding'
  }
  return statusMap[status] || 'status-default'
}
</script>

<template>
  <div class="order-container">
    <ReturnButton />
    <h1>我的订单</h1>
    <div v-if="orderList.length === 0" class="empty-order">
      <p>暂无订单记录</p>
      <router-link to="/">去购物</router-link>
    </div>

    <div v-else class="orders-grid">
      <div v-for="order in orderList" :key="order.id" class="order-card">
        <div class="order-header">
          <div class="order-meta">
            <span class="order-id">订单号: {{ order.id }}</span>
            <span class="order-time">下单时间: {{ order.orderTime }}</span>
          </div>
          <span class="status" :class="statusClass(order.status)">{{ order.status }}</span>
        </div>

        <div class="order-items">
          <div v-for="(item, index) in order.goodsList1" :key="index" class="item">
            <div class="item-info">
              <h3>{{ item.name }}</h3>
              <div class="item-meta">
                <span class="item-quantity">{{ item.quantity }} 件</span>
                <span class="item-spec" v-if="item.spec">规格: {{ item.spec }}</span>
              </div>
            </div>
            <div class="item-price">{{ formatPrice(item.price) }}</div>
          </div>
        </div>

        <div class="order-footer">
          <div class="actions">收货地址：{{ order.address }}</div>
          <div class="total">
            <span class="item-count">共 {{ order.goodsList1.length }} 件商品&nbsp;&nbsp;</span>
            总计: <strong>{{ formatPrice(order.totalPrice) }}</strong>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
:deep(.return-button) {
  margin-bottom: 20px;
}

.order-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  min-height: calc(100vh - 100px);
}

h1 {
  font-size: 28px;
  font-weight: 700;
  margin: 20px 0 30px;
  color: #333;
  text-align: center;
  position: relative;

  &::after {
    content: '';
    display: block;
    width: 60px;
    height: 3px;
    background: var(--theme-color);
    margin: 10px auto 0;
    border-radius: 2px;
  }
}

.empty-order {
  text-align: center;
  padding: 60px 0;
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.05);
  margin: 30px 0;
  border: 1px dashed #eee;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.08);
  }

  svg {
    margin-bottom: 20px;
    color: #ccc;
  }

  p {
    margin-bottom: 25px;
    font-size: 18px;
    color: #888;
  }

  a {
    display: inline-block;
    background: var(--theme-color);
    color: black;
    padding: 10px 28px;
    border-radius: 30px;
    text-decoration: none;
    font-weight: 600;
    font-size: 16px;
    transition: all 0.3s;
    box-shadow: 0 4px 12px rgba(255, 250, 107, 0.25);

    &:hover {
      color: #fff;
      background-color: #ff6b6b;
      transform: translateY(-2px);
      box-shadow: 0 6px 15px rgba(255, 107, 107, 0.35);
    }
  }
}

.orders-grid {
  display: grid;
  gap: 25px;
  grid-template-columns: 1fr;
}

.order-card {
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.06);
  background-color: white;
  transition: all 0.3s ease;
  border: 1px solid #f0f0f0;

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
    border-color: #e6e6e6;
  }
}

.order-header {
  background: linear-gradient(to right, #fafafa, #f5f5f5);
  padding: 16px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #eee;
}

.order-meta {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.order-id {
  font-size: 15px;
  font-weight: 600;
  color: #333;
}

.order-time {
  font-size: 13px;
  color: #888;
}

.status {
  font-weight: 600;
  padding: 5px 14px;
  border-radius: 30px;
  font-size: 13px;

  &.status-pending {
    background: rgba(255, 152, 0, 0.1);
    color: #ff9800;
    border: 1px solid rgba(255, 152, 0, 0.2);
  }

  &.status-paid {
    background: rgba(33, 150, 243, 0.1);
    color: #2196f3;
    border: 1px solid rgba(33, 150, 243, 0.2);
  }

  &.status-processing {
    background: rgba(156, 39, 176, 0.1);
    color: #9c27b0;
    border: 1px solid rgba(156, 39, 176, 0.2);
  }

  &.status-shipped {
    background: rgba(76, 175, 80, 0.1);
    color: #4caf50;
    border: 1px solid rgba(76, 175, 80, 0.2);
  }

  &.status-completed {
    background: rgba(96, 125, 139, 0.1);
    color: #607d8b;
    border: 1px solid rgba(96, 125, 139, 0.2);
  }

  &.status-canceled {
    background: rgba(158, 158, 158, 0.1);
    color: #9e9e9e;
    border: 1px solid rgba(158, 158, 158, 0.2);
  }

  &.status-refunding {
    background: rgba(244, 67, 54, 0.1);
    color: #f44336;
    border: 1px solid rgba(244, 67, 54, 0.2);
  }
}

.order-items {
  padding: 20px 24px;
  display: grid;
  gap: 18px;
  background: #fff;
}

.item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f8f8f8;

  &:last-child {
    border-bottom: none;
  }
}

.item-info {
  flex: 1;
  min-width: 0;

  h3 {
    margin: 0 0 8px;
    font-size: 16px;
    font-weight: 600;
    color: #333;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

.item-meta {
  display: flex;
  gap: 15px;
  font-size: 13px;
  color: #888;
}

.item-price {
  font-weight: 600;
  color: #e53935;
  font-size: 15px;
  min-width: 80px;
  text-align: right;
}

.order-footer {
  background: #fcfcfc;
  padding: 18px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 20px;
  border-top: 1px solid #f0f0f0;
}

.total {
  font-size: 16px;
  color: #666;
  display: flex;
  align-items: center;
  gap: 10px;

  strong {
    color: #e53935;
    font-size: 18px;
    font-weight: 700;
  }
}

.item-count {
  font-size: 13px;
  color: #999;
}

.actions {
  display: flex;
  gap: 12px;
  font-weight: bold;
  color: #333;
  font-size: 15px;
  flex-wrap: wrap;
}

.btn {
  padding: 8px 18px;
  border-radius: 30px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s ease;
  outline: none;
  font-weight: 500;
  border: 1px solid transparent;

  &-detail {
    background: #fff;
    color: #666;
    border-color: #ddd;

    &:hover {
      background: #f9f9f9;
      color: #333;
      border-color: #ccc;
    }
  }

  &-cancel {
    background: #fff;
    color: #f44336;
    border-color: #ffcdd2;

    &:hover {
      background: #ffebee;
      border-color: #f44336;
    }
  }

  &-pay {
    background: var(--theme-color);
    color: white;
    box-shadow: 0 3px 8px rgba(255, 107, 107, 0.3);

    &:hover {
      background-color: #ff6b6b;
      transform: translateY(-2px);
      box-shadow: 0 5px 12px rgba(255, 107, 107, 0.4);
    }
  }
}

// 响应式设计
@media (max-width: 992px) {
  .order-container {
    padding: 20px;
  }
}

@media (max-width: 576px) {
  .order-header {
    align-items: center;
    gap: 12px;
  }

  .item {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .item-price {
    align-self: flex-end;
  }

  .btn {
    padding: 7px 14px;
    font-size: 13px;
  }
}

@media (max-width: 480px) {
  .actions {
    flex-wrap: wrap;
    justify-content: center;

    .btn {
      flex: 1;
      text-align: center;
    }
  }

  .order-items {
    padding: 15px;
  }

  .order-header,
  .order-footer {
    padding: 14px 16px;
  }
}
</style>
